package com.atguigu.zhxy.controller;

import com.atguigu.zhxy.pojo.Grade;
import com.atguigu.zhxy.service.GradeService;
import com.atguigu.zhxy.util.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "年级控制器")
@RestController
@RequestMapping("/sms/gradeController")
public class GradeController {
    @Autowired
    private GradeService gradeService ;

    @ApiOperation("新增或修改Grade，有id属性就是修改，没有则是新增")
    @PostMapping("/saveOrUpdateGrade")
    public Result saveOrUpdateGrade(
            @ApiParam("Json的grade对象")@RequestBody Grade grade){
        gradeService.saveOrUpdate(grade) ;
        return Result.ok();
    }

    @ApiOperation("获取全部年级")
    @GetMapping("/getGrades")
    public Result getGrades(){
        List<Grade> grades =  gradeService.getGrades();
        return Result.ok(grades) ;
    }

    @ApiOperation("根据年级名称模糊查询，带分页")
    @GetMapping("/getGrades/{pageNum}/{pageSize}")
    public Result getGrades(
            @ApiParam("分页查询的页码数")@PathVariable("pageNum") Integer pageNum,
            @ApiParam("分页查询的记录数")@PathVariable("pageSize") Integer pageSize,
            @ApiParam("分页查询模糊匹配的名称")@Param("gradeName") String gradeName
    ){
//        分页带条件查询
        Page<Grade> page = new Page<>(pageNum, pageSize);
//        通过服务层
        IPage<Grade> gradeIPage = gradeService.getGradesByPN(page,gradeName);
//        封装result对象并返回
        return Result.ok(gradeIPage) ;
    }

    @ApiOperation("删除Grade")
    @DeleteMapping("/deleteGrade")
    public Result deleteGrade(
            @ApiParam("要删除的所有的grade的id的JSON集合") @RequestBody ArrayList<Integer> idList){
        gradeService.removeByIds(idList) ;
        return Result.ok();
    }


}
